﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameInput;

namespace Terraria.UI
{
	// Token: 0x02000120 RID: 288
	public class UserInterface
	{
		// Token: 0x170000DB RID: 219
		// (get) Token: 0x06000CAF RID: 3247 RVA: 0x0000CD62 File Offset: 0x0000AF62
		public UIState CurrentState
		{
			get
			{
				return this._currentState;
			}
		}

		// Token: 0x06000CB0 RID: 3248 RVA: 0x0000CD6A File Offset: 0x0000AF6A
		public void ResetLasts()
		{
			this._lastElementHover = null;
			this._lastElementDown = null;
			this._lastElementRightDown = null;
			this._lastElementClicked = null;
			this._lastElementRightClicked = null;
		}

		// Token: 0x06000CB1 RID: 3249 RVA: 0x0000CD8F File Offset: 0x0000AF8F
		public UserInterface()
		{
			UserInterface.ActiveInstance = this;
		}

		// Token: 0x06000CB2 RID: 3250 RVA: 0x0000CDA8 File Offset: 0x0000AFA8
		public void Use()
		{
			if (UserInterface.ActiveInstance != this)
			{
				UserInterface.ActiveInstance = this;
				this.Recalculate();
				return;
			}
			UserInterface.ActiveInstance = this;
		}

		// Token: 0x06000CB3 RID: 3251 RVA: 0x003F4ACC File Offset: 0x003F2CCC
		private void ResetState()
		{
			this.MousePosition = new Vector2((float)Main.mouseX, (float)Main.mouseY);
			this._wasMouseDown = Main.mouseLeft;
			this._wasMouseRightDown = Main.mouseRight;
			this._wasMouseMiddleDown = Main.mouseMiddle;
			this._wasMouseXButton1Down = Main.mouseXButton1;
			this._wasMouseXButton2Down = Main.mouseXButton2;
			if (this._lastElementHover != null)
			{
				this._lastElementHover.MouseOut(new UIMouseEvent(this._lastElementHover, this.MousePosition));
			}
			this._lastElementHover = null;
			this._lastElementDown = null;
			this._lastElementRightDown = null;
			this._lastElementClicked = null;
			this._lastElementRightClicked = null;
			this._lastMouseDownTime = 0.0;
			this._lastMouseRightDownTime = 0.0;
			this._lastMouseMiddleDownTime = 0.0;
			this._lastMouseXButton1DownTime = 0.0;
			this._lastMouseXButton2DownTime = 0.0;
			this._clickDisabledTimeRemaining = Math.Max(this._clickDisabledTimeRemaining, 200.0);
		}

		// Token: 0x06000CB4 RID: 3252 RVA: 0x003F4BD4 File Offset: 0x003F2DD4
		public void Update(GameTime time)
		{
			if (this._currentState != null)
			{
				this.MousePosition = new Vector2((float)Main.mouseX, (float)Main.mouseY);
				bool flag = Main.mouseLeft && Main.hasFocus;
				bool flag2 = Main.mouseRight && Main.hasFocus;
				bool flag3 = Main.mouseMiddle && Main.hasFocus;
				bool flag4 = Main.mouseXButton1 && Main.hasFocus;
				bool flag5 = Main.mouseXButton2 && Main.hasFocus;
				UIElement uielement = Main.hasFocus ? this._currentState.GetElementAt(this.MousePosition) : null;
				this._clickDisabledTimeRemaining = Math.Max(0.0, this._clickDisabledTimeRemaining - time.ElapsedGameTime.TotalMilliseconds);
				bool flag6 = this._clickDisabledTimeRemaining > 0.0;
				if (uielement != this._lastElementHover)
				{
					if (this._lastElementHover != null)
					{
						this._lastElementHover.MouseOut(new UIMouseEvent(this._lastElementHover, this.MousePosition));
					}
					if (uielement != null)
					{
						uielement.MouseOver(new UIMouseEvent(uielement, this.MousePosition));
					}
					this._lastElementHover = uielement;
				}
				if (flag && !this._wasMouseDown && uielement != null && !flag6)
				{
					this._lastElementDown = uielement;
					uielement.MouseDown(new UIMouseEvent(uielement, this.MousePosition));
					if (this._lastElementClicked == uielement && time.TotalGameTime.TotalMilliseconds - this._lastMouseDownTime < 500.0)
					{
						uielement.DoubleClick(new UIMouseEvent(uielement, this.MousePosition));
						this._lastElementClicked = null;
					}
					this._lastMouseDownTime = time.TotalGameTime.TotalMilliseconds;
				}
				else if (!flag && this._wasMouseDown && this._lastElementDown != null && !flag6)
				{
					UIElement lastElementDown = this._lastElementDown;
					if (lastElementDown.ContainsPoint(this.MousePosition))
					{
						lastElementDown.Click(new UIMouseEvent(lastElementDown, this.MousePosition));
						this._lastElementClicked = this._lastElementDown;
					}
					lastElementDown.MouseUp(new UIMouseEvent(lastElementDown, this.MousePosition));
					this._lastElementDown = null;
				}
				if (flag2 && !this._wasMouseRightDown && uielement != null && !flag6)
				{
					this._lastElementRightDown = uielement;
					uielement.RightMouseDown(new UIMouseEvent(uielement, this.MousePosition));
					if (this._lastElementRightClicked == uielement && time.TotalGameTime.TotalMilliseconds - this._lastMouseRightDownTime < 500.0)
					{
						uielement.RightDoubleClick(new UIMouseEvent(uielement, this.MousePosition));
						this._lastElementRightClicked = null;
					}
					this._lastMouseRightDownTime = time.TotalGameTime.TotalMilliseconds;
				}
				else if (!flag2 && this._wasMouseRightDown && this._lastElementRightDown != null && !flag6)
				{
					UIElement lastElementRightDown = this._lastElementRightDown;
					if (lastElementRightDown.ContainsPoint(this.MousePosition))
					{
						lastElementRightDown.RightClick(new UIMouseEvent(lastElementRightDown, this.MousePosition));
						this._lastElementRightClicked = this._lastElementRightDown;
					}
					lastElementRightDown.RightMouseUp(new UIMouseEvent(lastElementRightDown, this.MousePosition));
					this._lastElementRightDown = null;
				}
				if (flag3 && !this._wasMouseMiddleDown && uielement != null && !flag6)
				{
					this._lastElementMiddleDown = uielement;
					uielement.MiddleMouseDown(new UIMouseEvent(uielement, this.MousePosition));
					if (this._lastElementMiddleClicked == uielement && time.TotalGameTime.TotalMilliseconds - this._lastMouseMiddleDownTime < 500.0)
					{
						uielement.MiddleDoubleClick(new UIMouseEvent(uielement, this.MousePosition));
						this._lastElementMiddleClicked = null;
					}
					this._lastMouseMiddleDownTime = time.TotalGameTime.TotalMilliseconds;
				}
				else if (!flag3 && this._wasMouseMiddleDown && this._lastElementMiddleDown != null && !flag6)
				{
					UIElement lastElementMiddleDown = this._lastElementMiddleDown;
					if (lastElementMiddleDown.ContainsPoint(this.MousePosition))
					{
						lastElementMiddleDown.MiddleClick(new UIMouseEvent(lastElementMiddleDown, this.MousePosition));
						this._lastElementMiddleClicked = this._lastElementMiddleDown;
					}
					lastElementMiddleDown.MiddleMouseUp(new UIMouseEvent(lastElementMiddleDown, this.MousePosition));
					this._lastElementMiddleDown = null;
				}
				if (flag4 && !this._wasMouseXButton1Down && uielement != null && !flag6)
				{
					this._lastElementXButton1Down = uielement;
					uielement.XButton1MouseDown(new UIMouseEvent(uielement, this.MousePosition));
					if (this._lastElementXButton1Clicked == uielement && time.TotalGameTime.TotalMilliseconds - this._lastMouseXButton1DownTime < 500.0)
					{
						uielement.XButton1DoubleClick(new UIMouseEvent(uielement, this.MousePosition));
						this._lastElementXButton1Clicked = null;
					}
					this._lastMouseXButton1DownTime = time.TotalGameTime.TotalMilliseconds;
				}
				else if (!flag4 && this._wasMouseXButton1Down && this._lastElementXButton1Down != null && !flag6)
				{
					UIElement lastElementXButton1Down = this._lastElementXButton1Down;
					if (lastElementXButton1Down.ContainsPoint(this.MousePosition))
					{
						lastElementXButton1Down.XButton1Click(new UIMouseEvent(lastElementXButton1Down, this.MousePosition));
						this._lastElementXButton1Clicked = this._lastElementXButton1Down;
					}
					lastElementXButton1Down.XButton1MouseUp(new UIMouseEvent(lastElementXButton1Down, this.MousePosition));
					this._lastElementXButton1Down = null;
				}
				if (flag5 && !this._wasMouseXButton2Down && uielement != null && !flag6)
				{
					this._lastElementXButton2Down = uielement;
					uielement.XButton2MouseDown(new UIMouseEvent(uielement, this.MousePosition));
					if (this._lastElementXButton2Clicked == uielement && time.TotalGameTime.TotalMilliseconds - this._lastMouseXButton2DownTime < 500.0)
					{
						uielement.XButton2DoubleClick(new UIMouseEvent(uielement, this.MousePosition));
						this._lastElementXButton2Clicked = null;
					}
					this._lastMouseXButton2DownTime = time.TotalGameTime.TotalMilliseconds;
				}
				else if (!flag5 && this._wasMouseXButton2Down && this._lastElementXButton2Down != null && !flag6)
				{
					UIElement lastElementXButton2Down = this._lastElementXButton2Down;
					if (lastElementXButton2Down.ContainsPoint(this.MousePosition))
					{
						lastElementXButton2Down.XButton2Click(new UIMouseEvent(lastElementXButton2Down, this.MousePosition));
						this._lastElementXButton2Clicked = this._lastElementXButton2Down;
					}
					lastElementXButton2Down.XButton2MouseUp(new UIMouseEvent(lastElementXButton2Down, this.MousePosition));
					this._lastElementXButton2Down = null;
				}
				if (PlayerInput.ScrollWheelDeltaForUI != 0 && uielement != null)
				{
					uielement.ScrollWheel(new UIScrollWheelEvent(uielement, this.MousePosition, PlayerInput.ScrollWheelDeltaForUI));
				}
				this._wasMouseDown = flag;
				this._wasMouseRightDown = flag2;
				this._wasMouseMiddleDown = flag3;
				this._wasMouseXButton1Down = flag4;
				this._wasMouseXButton2Down = flag5;
				if (this._currentState != null)
				{
					this._currentState.Update(time);
				}
			}
		}

		// Token: 0x06000CB5 RID: 3253 RVA: 0x0000CDC5 File Offset: 0x0000AFC5
		public void Draw(SpriteBatch spriteBatch, GameTime time)
		{
			this.Use();
			if (this._currentState != null)
			{
				this._currentState.Draw(spriteBatch);
			}
		}

		// Token: 0x06000CB6 RID: 3254 RVA: 0x0000CDE1 File Offset: 0x0000AFE1
		public void SetState(UIState state)
		{
			if (state != null)
			{
				this.AddToHistory(state);
			}
			if (this._currentState != null)
			{
				this._currentState.Deactivate();
			}
			this._currentState = state;
			this.ResetState();
			if (state != null)
			{
				state.Activate();
				state.Recalculate();
			}
		}

		// Token: 0x06000CB7 RID: 3255 RVA: 0x003F5244 File Offset: 0x003F3444
		public void GoBack()
		{
			if (this._history.Count < 2)
			{
				return;
			}
			UIState state = this._history[this._history.Count - 2];
			this._history.RemoveRange(this._history.Count - 2, 2);
			this.SetState(state);
		}

		// Token: 0x06000CB8 RID: 3256 RVA: 0x0000CE1C File Offset: 0x0000B01C
		private void AddToHistory(UIState state)
		{
			this._history.Add(state);
			if (this._history.Count > 32)
			{
				this._history.RemoveRange(0, 4);
			}
		}

		// Token: 0x06000CB9 RID: 3257 RVA: 0x0000CE46 File Offset: 0x0000B046
		public void Recalculate()
		{
			if (this._currentState != null)
			{
				this._currentState.Recalculate();
			}
		}

		// Token: 0x06000CBA RID: 3258 RVA: 0x0000CE5B File Offset: 0x0000B05B
		public CalculatedStyle GetDimensions()
		{
			return new CalculatedStyle(0f, 0f, (float)Main.screenWidth, (float)Main.screenHeight);
		}

		// Token: 0x06000CBB RID: 3259 RVA: 0x0000CE78 File Offset: 0x0000B078
		internal void RefreshState()
		{
			if (this._currentState != null)
			{
				this._currentState.Deactivate();
			}
			this.ResetState();
			this._currentState.Activate();
			this._currentState.Recalculate();
		}

		// Token: 0x06000CBC RID: 3260 RVA: 0x0000CEA9 File Offset: 0x0000B0A9
		public bool IsElementUnderMouse()
		{
			return this.IsVisible && this._lastElementHover != null && !(this._lastElementHover is UIState);
		}

		// Token: 0x0400104B RID: 4171
		private const double DOUBLE_CLICK_TIME = 500.0;

		// Token: 0x0400104C RID: 4172
		private const double STATE_CHANGE_CLICK_DISABLE_TIME = 200.0;

		// Token: 0x0400104D RID: 4173
		private const int MAX_HISTORY_SIZE = 32;

		// Token: 0x0400104E RID: 4174
		private const int HISTORY_PRUNE_SIZE = 4;

		// Token: 0x0400104F RID: 4175
		public static UserInterface ActiveInstance = new UserInterface();

		// Token: 0x04001050 RID: 4176
		private List<UIState> _history = new List<UIState>();

		// Token: 0x04001051 RID: 4177
		public Vector2 MousePosition;

		// Token: 0x04001052 RID: 4178
		private bool _wasMouseDown;

		// Token: 0x04001053 RID: 4179
		private bool _wasMouseRightDown;

		// Token: 0x04001054 RID: 4180
		private bool _wasMouseMiddleDown;

		// Token: 0x04001055 RID: 4181
		private bool _wasMouseXButton1Down;

		// Token: 0x04001056 RID: 4182
		private bool _wasMouseXButton2Down;

		// Token: 0x04001057 RID: 4183
		private UIElement _lastElementHover;

		// Token: 0x04001058 RID: 4184
		private UIElement _lastElementDown;

		// Token: 0x04001059 RID: 4185
		private UIElement _lastElementRightDown;

		// Token: 0x0400105A RID: 4186
		private UIElement _lastElementMiddleDown;

		// Token: 0x0400105B RID: 4187
		private UIElement _lastElementXButton1Down;

		// Token: 0x0400105C RID: 4188
		private UIElement _lastElementXButton2Down;

		// Token: 0x0400105D RID: 4189
		private UIElement _lastElementClicked;

		// Token: 0x0400105E RID: 4190
		private UIElement _lastElementRightClicked;

		// Token: 0x0400105F RID: 4191
		private UIElement _lastElementMiddleClicked;

		// Token: 0x04001060 RID: 4192
		private UIElement _lastElementXButton1Clicked;

		// Token: 0x04001061 RID: 4193
		private UIElement _lastElementXButton2Clicked;

		// Token: 0x04001062 RID: 4194
		private double _lastMouseDownTime;

		// Token: 0x04001063 RID: 4195
		private double _lastMouseRightDownTime;

		// Token: 0x04001064 RID: 4196
		private double _lastMouseMiddleDownTime;

		// Token: 0x04001065 RID: 4197
		private double _lastMouseXButton1DownTime;

		// Token: 0x04001066 RID: 4198
		private double _lastMouseXButton2DownTime;

		// Token: 0x04001067 RID: 4199
		private double _clickDisabledTimeRemaining;

		// Token: 0x04001068 RID: 4200
		public bool IsVisible;

		// Token: 0x04001069 RID: 4201
		private UIState _currentState;
	}
}
